#include "mesh2d.h"

using namespace ePhys;

mesh2d *meshLoader2d::makeMesh(tri2d *buf, u32 bufSize) {
	mesh2d *m = new mesh2d();
	m->setBuffer(buf, bufSize);
	return m;
}

void meshLoader2d::addTriangle(tri2d tri) {
	tris.push_back(&tri);
}

mesh2d *meshLoader2d::finish(void) {
	u32 size = (u32) tris.getSize();
	tri2d *buf = new tri2d[size];
	for(u32 i = 0; i < size; i++)
		buf[i].set(tris[i]);
	tris.empty();
	return makeMesh(buf, size);
}

mesh2d::mesh2d(void)
{
}


mesh2d::~mesh2d(void)
{
	if(buf)
		delete [] buf;
}

bool mesh2d::isInside(vec2d p) {
	p = p - pos; // make relative to local

	for(u32 i = 0; i < bufSize; i++)
		if(!buf[i].inTriangle(p))
			return false;
	return (bufSize > 0); //If we have any triangles; the points in 'em
}